package com.javaweb.admin.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.javaweb.admin.entity.NcTaskLog;
import com.javaweb.admin.entity.PurchaseinInspectionResult;
import com.javaweb.admin.entity.Scheduled;
import com.javaweb.admin.mapper.KdlMaraMapper;
import com.javaweb.admin.mapper.ScheduledMapper;
import com.javaweb.admin.query.KdlMaraQuery;
import com.javaweb.admin.query.KdlPurchaseinQuery;
import com.javaweb.admin.service.AbstractJob;
import com.javaweb.admin.service.IKdlGMService;
import com.javaweb.admin.service.INcTaskLogService;
import com.javaweb.common.config.CommonConfig;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.JsonResult;
import com.javaweb.common.utils.StringUtils;
import com.seegw.shaphar.IKDLService;
import org.checkerframework.checker.units.qual.A;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class SyncKdlMara extends AbstractJob {
    private static final Logger log = LoggerFactory.getLogger(SyncKdlMara.class);
    @Autowired
    ScheduledMapper scheduledMapper;

    @Autowired
    IKDLService kdlService;

    @Autowired
    INcTaskLogService iNcTaskLogService;

    @Autowired
    private KdlMaraMapper kdlMaraMapper;

    @Override
    public String getName() {
        QueryWrapper<Scheduled> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("classpath",this.getClass().getName());
        Scheduled entity = scheduledMapper.selectOne(queryWrapper);
        return entity.getName();
    }

    @Override
    public void execute() {
        //获取康德乐采购入库质检回传
        KdlMaraQuery query = new KdlMaraQuery();
        if(StringUtils.isEmpty(query.getPrincipalid()))
        {
            query.setPrincipalid(CommonConfig.kdlCompanyId);
        }

        query.setDoctype("MARA");
        query.setMasterdatano("");
        query.setMastertype("M");
        query.setEndtime(DateUtils.getTime());
        try {
            NcTaskLog taskLog = new NcTaskLog();
            taskLog.setTaskType("主数据审批回传");
            taskLog.setBillCount(0);
            taskLog.setStartTime(DateUtils.now());
            String billno = "";
            String remark = "";

            log.info("开始获取物料主数据审批回传....");
            query.setStarttime(kdlService.getMasterDataMaxSyncTime(query.getMastertype()));

            kdlService.syncMasterDataResult(query);
            log.info("获取物料主数据审批回传完成");

            query.setMastertype("V");
            query.setStarttime(kdlService.getMasterDataMaxSyncTime(query.getMastertype()));
            log.info("开始获取供应商主数据审批回传....");
            kdlService.syncMasterDataResult(query);
            log.info("获取供应商主数据审批回传完成");

            query.setMastertype("C");
            query.setStarttime(kdlService.getMasterDataMaxSyncTime(query.getMastertype()));
            log.info("开始获取客户主数据审批回传....");
            kdlService.syncMasterDataResult(query);
            log.info("获取客户主数据审批回传完成");

            taskLog.setBillNo(billno);
            taskLog.setEndTime(DateUtils.now());
            iNcTaskLogService.save(taskLog);
        }
        catch(Exception ex){
            log.error(ex.getMessage());
        }


    }
}
